package com.zbkj.crmeb.store.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.utils.DateUtil;
import com.zbkj.crmeb.enums.CrowdCharacteristicsConditionEnum;
import com.zbkj.crmeb.enums.CrowdCharacteristicsEnum;
import com.zbkj.crmeb.store.dao.StoreUserGroupDao;
import com.zbkj.crmeb.store.dto.StoreUserGroupFeatureDTO;
import com.zbkj.crmeb.store.model.StoreUserGroup;
import com.zbkj.crmeb.store.model.StoreUserGroupFeature;
import com.zbkj.crmeb.store.request.StoreUserGroupFeatureRequest;
import com.zbkj.crmeb.store.request.StoreUserGroupRequest;
import com.zbkj.crmeb.store.response.StoreUserGroupResponse;
import com.zbkj.crmeb.store.service.CrowdCharacteristicsOrderService;
import com.zbkj.crmeb.store.service.CrowdCharacteristicsUserService;
import com.zbkj.crmeb.store.service.StoreUserGroupFeatureService;
import com.zbkj.crmeb.store.service.StoreUserGroupService;
import com.zbkj.crmeb.system.model.SystemAdmin;
import com.zbkj.crmeb.system.service.SystemAdminService;
import com.zbkj.crmeb.user.model.User;
import com.zbkj.crmeb.user.service.UserService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StoreUserGroupServiceImpl extends ServiceImpl<StoreUserGroupDao, StoreUserGroup> implements StoreUserGroupService {

    @Autowired
    private StoreUserGroupDao storeUserGroupDao;
    @Autowired
    private SystemAdminService systemAdminService;
    @Autowired
    private StoreUserGroupFeatureService storeUserGroupFeatureService;

    @Autowired
    private CrowdCharacteristicsOrderService crowdCharacteristicsOrderService;
    @Autowired
    private CrowdCharacteristicsUserService crowdCharacteristicsUserService;
    @Autowired
    private UserService userService;

    @Autowired
    StoreUserGroupService storeUserGroupService;

    /**
     * 功能描述: 用于定时计算用户分群
     *
     * @Param:
     * @Return: void
     * @Author: duyb
     * @Date: 2022/10/19
     */
    public void calculationUserGroup() {
        //获取分群id
        List<StoreUserGroup> groupList = storeUserGroupService.list(Wrappers.<StoreUserGroup>lambdaQuery());

        List<Integer> merIds = groupList.stream().map(u -> u.getMerId()).distinct().collect(Collectors.toList());
        Map<Integer, List<User>> userMap = new HashMap<>();
        for (Integer merId : merIds) {
            List<User> userList = userService.list(Wrappers.<User>lambdaQuery()
                    .eq(User::getMerId, merId)
            );
            userMap.put(merId, userList);
        }

        if (CollectionUtils.isNotEmpty(groupList)) {
            for (StoreUserGroup storeUserGroup : groupList) {
                //获取商户下用户
                List<User> userList = JSONArray.parseArray(JSON.toJSONString(userMap.get(storeUserGroup.getMerId())), User.class);
//                List<User> userList = userService.list(Wrappers.<User>lambdaQuery()
//                        .eq(User::getMerId, storeUserGroup.getMerId())
//                );
                if (CollectionUtils.isEmpty(userList)) {
                    return;
                }
                //获取特征条件
                List<StoreUserGroupFeature> featureList = storeUserGroupFeatureService.list(Wrappers.<StoreUserGroupFeature>lambdaQuery()
                        .eq(StoreUserGroupFeature::getGroupId, storeUserGroup.getId())
                );
                if (CollectionUtils.isNotEmpty(featureList)) {
                    for (StoreUserGroupFeature storeUserGroupFeature : featureList) {
                        Integer code = storeUserGroupFeature.getGroupFeature().getCode();
                        switch (storeUserGroupFeature.getGroupFeature()) {
                            case HAVE_PURCHASE:
                            case NO_PURCHASE:
                                crowdCharacteristicsOrderService.SiftPurchase(userList, storeUserGroup.getId(), code, storeUserGroupFeature.getDay(), storeUserGroup.getMerId());
                                break;
                            case WITH_REPURCHASE:
                            case NO_REPURCHASE:
                                crowdCharacteristicsOrderService.SiftRePurchase(userList, storeUserGroup.getId(), code, storeUserGroupFeature.getDay(), storeUserGroup.getMerId());
                                break;
                            case HAVE_ACCESS:
                            case NO_ACCESS:
                                crowdCharacteristicsUserService.SiftAccess(userList, storeUserGroup.getId(), code, storeUserGroupFeature.getDay(), storeUserGroup.getMerId());
                                break;
                            case PAY_PRICE:
                                crowdCharacteristicsOrderService.SiftPayPrice(userList, storeUserGroup.getId(), code, storeUserGroup.getMerId(), storeUserGroupFeature.getJudgeCondition().getCode(), storeUserGroupFeature.getConditionOne(), storeUserGroupFeature.getConditionTwo());
                                break;
                            case PAY_TIME:
                                crowdCharacteristicsOrderService.SiftPayTime(userList, storeUserGroup.getId(), code, storeUserGroup.getMerId(), storeUserGroupFeature.getJudgeCondition().getCode(), storeUserGroupFeature.getConditionOne(), storeUserGroupFeature.getConditionTwo());
                                break;
                            case VIP_LEVEL:
                                crowdCharacteristicsUserService.MemberLevel(userList, storeUserGroup.getId(), code, storeUserGroupFeature.getConditionOne(), storeUserGroup.getMerId());
                                break;
                            case PAY_VIP:
                                crowdCharacteristicsUserService.WhetherPaidMember(userList, storeUserGroup.getId(), code, storeUserGroupFeature.getJudgeCondition().getCode(), storeUserGroupFeature.getConditionOne(), storeUserGroup.getMerId());
                                break;
                            case BALANCE:
                                crowdCharacteristicsUserService.SiftBalance(userList, storeUserGroup.getId(), code, storeUserGroupFeature.getJudgeCondition().getCode(), storeUserGroupFeature.getConditionOne(), storeUserGroupFeature.getConditionTwo(), storeUserGroup.getMerId());
                                break;
                        }
                    }
                }
                //把featureIds设置到user
                List<String> featureIds = null;
                if (CollectionUtils.isNotEmpty(userList)) {
                    for (User user : userList) {
                        if (StringUtil.isNotBlank(user.getFeatureId())) {
                            featureIds = new ArrayList<>(Arrays.asList(user.getFeatureId().split(",")));
                            if (!featureIds.contains(String.valueOf(storeUserGroup.getId()))) {
                                featureIds.add(String.valueOf(storeUserGroup.getId()));
                            }
                        } else {
                            featureIds = new ArrayList<>();
                            featureIds.add(String.valueOf(storeUserGroup.getId()));
                        }
                        userService.update(new UpdateWrapper<User>().lambda()
                                .eq(User::getUid, user.getUid())
                                .set(User::getFeatureId, featureIds.stream().collect(Collectors.joining(","))));
                    }
                    storeUserGroup.setPeopleNumber(userList.size());
                } else {
                    storeUserGroup.setPeopleNumber(0);
                }
                storeUserGroupService.saveOrUpdate(storeUserGroup);
            }
        }
    }

    /**
     * 功能描述: 返回当前用户所属商户下的所有分群id与名称
     *
     * @Param: [merId]
     * @Return: java.util.Map<java.lang.Integer, java.lang.String>
     * @Author: wangzh
     * @Date: 2022/9/30 10:39
     */
    @Override
    public Map<Integer, String> getGroupName(Integer merId) {
        List<StoreUserGroup> list = this.list(Wrappers.<StoreUserGroup>lambdaQuery()
                .eq(StoreUserGroup::getMerId, merId)
                .orderByDesc(StoreUserGroup::getUpdateTime));

        if (list.size() == 0) {
            return null;
        }

        return list.stream().collect(Collectors.toMap(StoreUserGroup::getId, StoreUserGroup::getGroupName));
    }

    /**
     * 功能描述: 保存/更新特征与条件
     *
     * @Param: [request, merId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2022/9/30 13:21
     */
    @Override
    public Boolean saveGroup(StoreUserGroupRequest request, Integer merId) {

        if (request.getStoreUserGroupFeatureRequests().size() == 0) {
            throw new CrmebException("特征不能为空");
        }
        //该商户下分群名称
        List<String> groupNameList = new ArrayList<>();
        Map<Integer, String> groupNameMap = getGroupName(merId);
        if (CollectionUtils.isNotEmpty(groupNameMap)) {
            for (Integer i : groupNameMap.keySet()) {
                if (Objects.equals(i, request.getId()) && request.getId() != null){
                    continue;
                }
                groupNameList.add(groupNameMap.get(i));
            }
        }

        if (groupNameList.contains(request.getGroupName())) {
            throw new CrmebException("该分群名称已存在");
        }

        //保存特征
        StoreUserGroup storeUserGroup = new StoreUserGroup();
        storeUserGroup.setGroupName(request.getGroupName())
                .setCreatorUserId(systemAdminService.getAdminId())
                .setMerId(merId)
                .setId(request.getId());
        //分群id为空，新增操作，给创建时间赋值
        if (null == request.getId()) {
            storeUserGroup.setCreateTime(DateUtil.nowDateTime());
        }
        //不为空，修改操作，给更新时间赋值
        storeUserGroup.setUpdateTime(DateUtil.nowDateTime());

        //先保存，获取分群id，再存入子表中
        boolean saveGroup = this.saveOrUpdate(storeUserGroup);

        //获取特征列表
        List<StoreUserGroupFeatureRequest> storeUserGroupFeatureRequests = request.getStoreUserGroupFeatureRequests();
        //计算人数
        storeUserGroup.setPeopleNumber(calculatePeople(merId, storeUserGroup.getId(), storeUserGroupFeatureRequests));
        //保存人数
        boolean saveGroupPeopleNum = this.saveOrUpdate(storeUserGroup);

        List<StoreUserGroupFeature> storeUserGroupFeatures = new ArrayList<>();
        List<Integer> storeUserGroupFeaturesIds = new ArrayList<>();
        //获取特征列表中id是否有值
        if (storeUserGroupFeatureRequests.size() != 0) {
            storeUserGroupFeaturesIds = storeUserGroupFeatureRequests.stream().map(StoreUserGroupFeatureRequest::getId).filter(Objects::nonNull).collect(Collectors.toList());
        }
        //将用户删除的特征从数据库中删除
        if (CollectionUtils.isNotEmpty(storeUserGroupFeatureRequests)) {
            storeUserGroupFeatureService.remove(Wrappers.<StoreUserGroupFeature>lambdaQuery()
                    .eq(StoreUserGroupFeature::getGroupId, request.getId())
                    .notIn(CollectionUtils.isNotEmpty(storeUserGroupFeaturesIds), StoreUserGroupFeature::getId, storeUserGroupFeaturesIds));
        }
        //循环赋值
        for (StoreUserGroupFeatureRequest storeUserGroupFeatureRequest : storeUserGroupFeatureRequests) {
            StoreUserGroupFeature storeUserGroupFeature = new StoreUserGroupFeature();
            BeanUtils.copyProperties(storeUserGroupFeatureRequest, storeUserGroupFeature);

            storeUserGroupFeature.setGroupFeature(CrowdCharacteristicsEnum.valueOf(storeUserGroupFeatureRequest.getGroupFeature()));
            storeUserGroupFeature.setGroupId(storeUserGroup.getId());
            if (storeUserGroupFeatureRequest.getJudgeCondition() != null) {
                storeUserGroupFeature.setJudgeCondition(CrowdCharacteristicsConditionEnum.valueOf(storeUserGroupFeatureRequest.getJudgeCondition()));
            }
            //id不为空为修改
            if (storeUserGroupFeature.getId() == null) {
                storeUserGroupFeature.setCreateTime(DateUtil.nowDateTime());
            }
            storeUserGroupFeature.setUpdateTime(DateUtil.nowDateTime());

            storeUserGroupFeatures.add(storeUserGroupFeature);
        }
        //保存特征
        boolean saveCondition = storeUserGroupFeatureService.saveOrUpdateBatch(storeUserGroupFeatures);
        return saveGroup && saveCondition && saveGroupPeopleNum;
    }

    /**
     * 功能描述: 分页查询分群信息
     *
     * @Param: [request, merId]
     * @Return: java.util.List<com.zbkj.crmeb.store.response.StoreUserGroupResponse>
     * @Author: wangzh
     * @Date: 2022/9/30 15:15
     */
    @Override
    public CommonPage<StoreUserGroupResponse> getList(PageParamRequest request, Integer merId) {
        PageHelper.startPage(request.getPage(), request.getLimit());
        List<StoreUserGroupResponse> storeUserGroupResponses = new ArrayList<>();
        //分页获取到的分群信息
        List<StoreUserGroup> storeUserGroups = this.list(Wrappers.<StoreUserGroup>lambdaQuery()
                .eq(StoreUserGroup::getMerId, merId)
                .orderByDesc(StoreUserGroup::getUpdateTime));

        if (storeUserGroups.size() == 0) {
            return null;
        }
        //对总页数进行处理，无法使用封装的restPage
        PageInfo<StoreUserGroup> pageInfo = new PageInfo<StoreUserGroup>(storeUserGroups);
        CommonPage<StoreUserGroupResponse> storeUserGroupResponseCommonPage = new CommonPage<>();
        storeUserGroupResponseCommonPage.setPage(pageInfo.getPageNum());
        storeUserGroupResponseCommonPage.setTotalPage(pageInfo.getPages());
        storeUserGroupResponseCommonPage.setLimit(pageInfo.getPageSize());
        storeUserGroupResponseCommonPage.setTotal(pageInfo.getTotal());

        for (StoreUserGroup storeUserGroup : storeUserGroups) {
            //将信息封装入StoreUserGroupResponse
            StoreUserGroupResponse storeUserGroupResponse = new StoreUserGroupResponse();
            List<StoreUserGroupFeature> storeUserGroupFeatures = storeUserGroupFeatureService.list(Wrappers.<StoreUserGroupFeature>lambdaQuery()
                    .eq(StoreUserGroupFeature::getGroupId, storeUserGroup.getId()));

            if (storeUserGroupFeatures.size() == 0) {
                return null;
            }
            //将对象复制入dto，同时将特征码转化为文字
            List<StoreUserGroupFeatureDTO> storeUserGroupFeatureDTOS = new ArrayList<>();
            for (StoreUserGroupFeature storeUserGroupFeature : storeUserGroupFeatures) {
                StoreUserGroupFeatureDTO storeUserGroupFeatureDTO = new StoreUserGroupFeatureDTO();
                BeanUtils.copyProperties(storeUserGroupFeature, storeUserGroupFeatureDTO);
                storeUserGroupFeatureDTO.setGroupFeature(storeUserGroupFeature.getGroupFeature().getMsg());
                storeUserGroupFeatureDTO.setJudgeCondition(storeUserGroupFeature.getJudgeCondition() != null ? storeUserGroupFeature.getJudgeCondition().getMsg() : null);
                storeUserGroupFeatureDTOS.add(storeUserGroupFeatureDTO);
            }
            //查询用户名字
            SystemAdmin admin = null;
            if (storeUserGroup.getCreatorUserId() != null) {
                admin = systemAdminService.getById(storeUserGroup.getCreatorUserId());
            }

            storeUserGroupResponse.setGroupType(storeUserGroupFeatureDTOS)
                    .setGroupNum(storeUserGroup.getPeopleNumber())
                    .setGroupName(storeUserGroup.getGroupName())
                    .setUpdateTime(storeUserGroup.getUpdateTime())
                    .setCreatorUserName(admin == null ? null : admin.getAccount())
                    .setId(storeUserGroup.getId());
            storeUserGroupResponses.add(storeUserGroupResponse);
        }
        storeUserGroupResponseCommonPage.setList(storeUserGroupResponses);
        return storeUserGroupResponseCommonPage;
    }

    /**
     * 功能描述: 根据分群id删除
     *
     * @Param: [id]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2022/9/30 15:36
     */
    @Override
    public Boolean deleteGroup(Integer id) {

        boolean group = this.removeById(id);
        boolean condition = storeUserGroupFeatureService.remove(Wrappers.<StoreUserGroupFeature>lambdaQuery()
                .eq(StoreUserGroupFeature::getGroupId, id));

        return group && condition;
    }


    /**
     * 功能描述: 计算人数
     *
     * @Param: [merId storeUserGroupFeatureRequestList]
     * @Return: Integer
     * @Author: duyb
     * @Date: 2022/10/08 09:58
     */
    @Override
    public Integer calculatePeople(Integer merId, Integer groupId, List<StoreUserGroupFeatureRequest> storeUserGroupFeatureRequestList) {
        //获取商户下用户
        List<User> userList = userService.list(Wrappers.<User>lambdaQuery()
                .eq(User::getMerId, merId)
        );

        if (storeUserGroupFeatureRequestList != null && storeUserGroupFeatureRequestList.size() > 0) {
            for (StoreUserGroupFeatureRequest storeUserGroupFeatureRequest : storeUserGroupFeatureRequestList) {
                if (storeUserGroupFeatureRequest.getGroupFeature() == null) {
                    throw new CrmebException("特征不能为空");
                }
                Integer code = storeUserGroupFeatureRequest.getGroupFeature();
                switch (CrowdCharacteristicsEnum.valueOf(storeUserGroupFeatureRequest.getGroupFeature())) {
                    case HAVE_PURCHASE:
                    case NO_PURCHASE:
                        crowdCharacteristicsOrderService.SiftPurchase(userList, groupId, code, storeUserGroupFeatureRequest.getDay(), merId);
                        break;
                    case WITH_REPURCHASE:
                    case NO_REPURCHASE:
                        crowdCharacteristicsOrderService.SiftRePurchase(userList, groupId, code, storeUserGroupFeatureRequest.getDay(), merId);
                        break;
                    case HAVE_ACCESS:
                    case NO_ACCESS:
                        crowdCharacteristicsUserService.SiftAccess(userList, groupId, code, storeUserGroupFeatureRequest.getDay(), merId);
                        break;
                    case PAY_PRICE:
                        crowdCharacteristicsOrderService.SiftPayPrice(userList, groupId, code, merId, storeUserGroupFeatureRequest.getJudgeCondition(), storeUserGroupFeatureRequest.getConditionOne(), storeUserGroupFeatureRequest.getConditionTwo());
                        break;
                    case PAY_TIME:
                        crowdCharacteristicsOrderService.SiftPayTime(userList, groupId, code, merId, storeUserGroupFeatureRequest.getJudgeCondition(), storeUserGroupFeatureRequest.getConditionOne(), storeUserGroupFeatureRequest.getConditionTwo());
                        break;
                    case VIP_LEVEL:
                        crowdCharacteristicsUserService.MemberLevel(userList, groupId, code, storeUserGroupFeatureRequest.getConditionOne(), merId);
                        break;
                    case PAY_VIP:
                        crowdCharacteristicsUserService.WhetherPaidMember(userList, groupId, code, storeUserGroupFeatureRequest.getJudgeCondition(), storeUserGroupFeatureRequest.getConditionOne(), merId);
                        break;
                    case BALANCE:
                        crowdCharacteristicsUserService.SiftBalance(userList, groupId, code, storeUserGroupFeatureRequest.getJudgeCondition(), storeUserGroupFeatureRequest.getConditionOne(), storeUserGroupFeatureRequest.getConditionTwo(), merId);
                        break;
                }
            }
        }
        List<String> featureIds = null;
        if (CollectionUtils.isNotEmpty(userList) && groupId != 0) {

            for (User user : userList) {
                if (StringUtil.isNotBlank(user.getFeatureId())) {
                    featureIds = new ArrayList<>(Arrays.asList(user.getFeatureId().split(",")));
                    if (!featureIds.contains(String.valueOf(groupId))) {
                        featureIds.add(String.valueOf(groupId));
                    }
                } else {
                    featureIds = new ArrayList<>();
                    featureIds.add(String.valueOf(groupId));
                }
                userService.update(new UpdateWrapper<User>().lambda()
                        .eq(User::getUid, user.getUid())
                        .set(User::getFeatureId, featureIds.stream().collect(Collectors.joining(","))));
            }
            return userList.size();
        } else if (CollectionUtils.isNotEmpty(userList) && groupId == 0) {
            return userList.size();
        } else {
            return 0;
        }


    }

    /**
     * 功能描述: 根据分群id获取信息
     *
     * @Param: [groupId, merId]
     * @Return: com.zbkj.crmeb.store.response.StoreUserGroupResponse
     * @Author: wangzh
     * @Date: 2022/10/18 13:49
     */
    @Override
    public StoreUserGroupResponse getByGroupId(Integer groupId, Integer merId) {
        StoreUserGroupResponse storeUserGroupResponse = new StoreUserGroupResponse();
        //根据id获取分群信息
        StoreUserGroup storeUserGroup = this.getOne(Wrappers.<StoreUserGroup>lambdaQuery()
                .eq(StoreUserGroup::getId, groupId)
                .eq(StoreUserGroup::getMerId, merId));
        if (Objects.isNull(storeUserGroup)) {
            return null;
        }
        //将信息封装如response对象中
        storeUserGroupResponse.setId(storeUserGroup.getId())
                .setGroupName(storeUserGroup.getGroupName())
                .setGroupNum(storeUserGroup.getPeopleNumber())
                .setUpdateTime(storeUserGroup.getUpdateTime());

        //查询管理员信息
        SystemAdmin admin = null;
        if (storeUserGroup.getCreatorUserId() != null) {
            admin = systemAdminService.getById(storeUserGroup.getCreatorUserId());
        }
        //将创建人id修改为姓名
        storeUserGroupResponse.setCreatorUserName(admin == null ? null : admin.getAccount());
        List<StoreUserGroupFeature> storeUserGroupFeatures = storeUserGroupFeatureService.list(Wrappers.<StoreUserGroupFeature>lambdaQuery()
                .eq(StoreUserGroupFeature::getGroupId, groupId));

        List<StoreUserGroupFeatureDTO> storeUserGroupFeatureDTOS = new ArrayList<>();
        for (StoreUserGroupFeature storeUserGroupFeature : storeUserGroupFeatures) {
            StoreUserGroupFeatureDTO storeUserGroupFeatureDTO = new StoreUserGroupFeatureDTO();
            BeanUtils.copyProperties(storeUserGroupFeature, storeUserGroupFeatureDTO);
            storeUserGroupFeatureDTO.setGroupFeature(storeUserGroupFeature.getGroupFeature().getMsg());
            storeUserGroupFeatureDTO.setJudgeCondition(storeUserGroupFeature.getJudgeCondition() != null ? storeUserGroupFeature.getJudgeCondition().getMsg() : null);
            storeUserGroupFeatureDTOS.add(storeUserGroupFeatureDTO);
        }
        storeUserGroupResponse.setGroupType(storeUserGroupFeatureDTOS);

        return storeUserGroupResponse;
    }

    /**
     * 功能描述: 根据商户id修改分群人数
     *
     * @Param: [groupId, merId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2022/10/20 16:37
     */
    @Override
    public Boolean updateGroupPeopleNumById(Integer groupId, Integer merId) {

        //该商户下所有分群id
        List<StoreUserGroup> storeUserGroups = this.list(Wrappers.<StoreUserGroup>lambdaQuery()
                .eq(StoreUserGroup::getMerId, merId));
        List<Integer> groupIds = new ArrayList<>();
        if (Objects.nonNull(storeUserGroups)) {
            groupIds = storeUserGroups.stream().map(StoreUserGroup::getId).collect(Collectors.toList());
        }

        if (!groupIds.contains(groupId)) {
            throw new CrmebException("该商户没有该分群");
        }

        //该分群下所有特征
        List<StoreUserGroupFeature> storeUserGroupFeatures = storeUserGroupFeatureService.list(Wrappers.<StoreUserGroupFeature>lambdaQuery()
                .eq(StoreUserGroupFeature::getGroupId, groupId));
        if (Objects.isNull(storeUserGroupFeatures)) {
            throw new CrmebException("分群下无任何特征");
        }
        List<StoreUserGroupFeatureRequest> storeUserGroupFeatureRequests = new ArrayList<>();
        for (StoreUserGroupFeature s : storeUserGroupFeatures) {
            StoreUserGroupFeatureRequest request = new StoreUserGroupFeatureRequest();
            BeanUtils.copyProperties(s, request);
            request.setGroupFeature(s.getGroupFeature().getCode());
            if (Objects.nonNull(s.getJudgeCondition())) {
                request.setJudgeCondition(s.getJudgeCondition().getCode());
            }
            storeUserGroupFeatureRequests.add(request);
        }
        Integer peopleNum = calculatePeople(merId, groupId, storeUserGroupFeatureRequests);
        StoreUserGroup storeUserGroup = new StoreUserGroup();
        storeUserGroup.setId(groupId).setPeopleNumber(peopleNum).setUpdateTime(new Date());

        return this.updateById(storeUserGroup);
    }

}
